﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using Rodunu.Business.Interfaces;
using Rodunu.Business.Models.User;
using Rodunu.Clients.WPF.Search;
using Rodunu.Services.NetFlix;
using Rodunu.Business.Models.Movies;
using Rodunu.Services.Interfaces;

namespace Rodunu.Clients.WPF
{
	/// <summary>
	/// Interaction logic for NetflixSearch.xaml
	/// </summary>
    public partial class NetflixSearch : ISearchControl
    {
        private readonly string _searchFor;
        private readonly BackgroundWorker _backgroundWorker;
        private readonly StackPanel _moviesInNetflix;
        private readonly Account _account;
        private double _height;
        private readonly ScrollViewer _scrollViewer;

        public NetflixSearch(string searchFor, Account account, double height)
		{
            InitializeComponent();

            _height = height;
            _account = account;
            _searchFor = searchFor;
            _scrollViewer = new ScrollViewer();

            _backgroundWorker = new BackgroundWorker();
            _backgroundWorker.DoWork += _backgroundWorker_DoWork;
            _backgroundWorker.RunWorkerCompleted += _backgroundWorker_RunWorkerCompleted;
            _backgroundWorker.RunWorkerAsync(_searchFor);

            _moviesInNetflix = new StackPanel();
            _moviesInNetflix.Margin = new Thickness(4, 0, 2, 0);

        }

        void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            NumberOfResults.Content = ((IList<SimpleID>)e.Result).Count;

            if (((IList<SimpleID>)e.Result).Count > 9)
            {
                _scrollViewer.Content = _moviesInNetflix;
                _scrollViewer.Margin = new Thickness(0, 2, -5, 0);

                LayoutRoot.Children.Add(_scrollViewer);
                Grid.SetRow(_scrollViewer, 1);
            }
            else
            {
                LayoutRoot.Children.Add(_moviesInNetflix);
                Grid.SetRow(_moviesInNetflix, 1);
            }

            LayoutRoot.Children.Remove(Loader);

            foreach (var item in (IList<SimpleID>)e.Result)
            {
                _moviesInNetflix.Children.Add(new SearchResultButton(item.Name, Convert.ToInt32(item.Id)));
            }
            if (_height > 30)
                _scrollViewer.Height = _height - 40;
            else
                _scrollViewer.Height = 40;
        }

        void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            ISimpleIDSearch simpleIDSearch = new NetFlixService(_account.Login, _account.Password);
            e.Result = simpleIDSearch.GetQuickResults(Convert.ToString(e.Argument));
        }

        #region ISearchControl Members

        public void SetHeight(double height)
        {
            _height = height;
            UserControl.Height = _height;
            if (_height > 30)
                _scrollViewer.Height = _height - 40;
            else
                _scrollViewer.Height = 40;
        }

        #endregion
    }
}